package com.zw.pm.module.config.factory;

import com.zw.pm.base.BaseController;
import com.zw.pm.common.JsonResult;
import com.zw.pm.common.ValidResult;
import com.zw.pm.gen.entity.Factory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description: 分厂 控制层
 *
 * @Author: China.sgl
 * @Date: 2019/11/28 13:48
 */
@Controller
@RequestMapping("/config/factory")
public class FactoryController extends BaseController {

    @Autowired
    private FactoryService factoryService;

    /**
     * 分厂页面
     * @return 页面路径
     */
    @GetMapping
    @RequiresPermissions({"config:factory"})
    public String page() {
        return "config/factory/page";
    }

    /**
     * 系统分厂数据
     * @return Json 数据
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"config:factory"})
    public JsonResult list(Factory factory) {
        startPage();
        List<Factory> list = factoryService.findFactoryAll(factory);
        return jsonResult(list);
    }

    /**
     * 名称唯一验证
     * @param factory factory
     * @return json
     */
    @ResponseBody
    @PostMapping("/checkUnique")
    @RequiresPermissions({"config:factory"})
    public ValidResult checkUnique(Factory factory) {
        Factory p = factoryService.findByFactoryName(factory.getName());
        if (factory.getId() == null) {
            return ValidResult.result(p);
        }
        if (p == null || factory.getId().equals(p.getId())) {
            return ValidResult.SUCCESS;
        }
        return ValidResult.FAIL;
    }

    /**
     * 创建分厂
     * @param factory factory
     * @return json
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"config:factory"})
    public JsonResult save(Factory factory) {
        return jsonResult(factoryService.saveFactory(factory));
    }

    /**
     * 根据ID查询
     * @param id id
     * @return Factory
     */
    @ResponseBody
    @PostMapping("/id")
    @RequiresPermissions({"config:factory"})
    public Factory id(Integer id) {
        return factoryService.getFactory(id);
    }

    /**
     * 修改分厂
     * @param factory factory
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions({"config:factory"})
    public JsonResult update(Factory factory) {
        return jsonResult(factoryService.updateFactory(factory));
    }

    /**
     * 删除
     *
     * @param factory factory
     * @return json
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"config:factory"})
    public JsonResult del(Factory factory) {
        return jsonResult(factoryService.delFactory(factory));
    }

}
